AddCSLuaFile( "sql_database.lua" )
include( 'sql_database.lua' )

-- Create a 2-dimensional table. The SteamID (aka unique_id) is used as the identifier and returns a table with player data.
-- [ply:SteamID()]['unique_id']
-- [ply:SteamID()]['money']
-- [ply:SteamID()]['stamina']
-- [ply:SteamID()]['strength']
-- [ply:SteamID()]['hp']
local stats = { }

--function outbreakStatsInit() -- Check our DataBase when initializing the game mode.
function GM:Initialize()
	if !outbreakSqlValidateInfoTable() then
		outbreakSqlCreateInfoTable()
	end
	print(outbreakSqlValidateInfoTable())
	
	if !outbreakSqlValidateSkillsTable() then
		outbreakSqlCreateSkillsTable()
	end
	print(outbreakSqlValidateSkillsTable())
end
--hook.Add( "Initialize", "outbreakStatsInit", outbreakStatsInit ) -- This hook has been disabled because this function is called from GM:Initialize()

--function outbreakStatsInitialSpawn( ply )
function GM:PlayerInitialSpawn( ply )  -- When spawning after joining the sever
	ply:Spectate( 4 ) -- Set him to spectate in free-roam mode. He doesn't actually fly around, since he has a window open at this point.
	ply:SetTeam( 1 ) -- Set his team to Joining
	
	data = outbreakSqlSelectPlayer(ply)
	
	if !data then
		data = outbreakSqlCreatePlayer(ply)
		print("Created a new player.")
	end
	
	if data then
		stats[ply:SteamID()] = data
	else
		print("Big freakin error")
	end
	
	set_team() -- Run the function defined in cl_init.lua. 	
end
--hook.Add( "PlayerInitialSpawn", "outbreakStatsInitialSpawn", outbreakStatsInitialSpawn ) -- This hook has been disabled because this function is called from GM:PlayerInitialSpawn( ply )

-- Grants an user 1 exp point toward their strength skill? when they kill a zombie
function GM:OnNPCKilled( victim, ply, weapon )
	if(victim:GetClass( ) == "npc_zombie" ) then
		print(stats[ply:SteamID()]['strength'])
		stats[ply:SteamID()]['strength'] += 1
		outbreakSqlUpdatePlayer(ply, stats[ply:SteamID()])
	end
end

-- What happens when a player spawns
function GM:PlayerSpawn( ply )
    self.BaseClass:PlayerSpawn( ply )   
 
    ply:SetGravity  ( 1 ) 
    ply:SetMaxHealth( 100, true )  
 
    ply:SetWalkSpeed( 325 )  
    ply:SetRunSpeed ( 325 ) 
end

-- What should the player recieve when joining a team?
function GM:PlayerLoadout( ply ) 
	if ply:Team() == 2 then 
		Scavanger( ply )
	elseif ply:Team() == 3 then 
		Scout( ply )
	else
		ply:Give( "weapon_crowbar" )
	end -- right here.
end 

function sb_team1( ply ) 
	Scavanger( ply ) 
	ply:Spawn() -- Spawn him
	ply:PrintMessage( HUD_PRINTTALK, "[Outbreak] I recognize you as a Scavanger " .. ply:Nick() ) -- Again, a message in the talk area. 
	func() 
end 

function sb_team2( ply )
	Scout( ply )
	ply:Spawn() -- Spawn him
	ply:PrintMessage( HUD_PRINTTALK, "[Outbreak] I recognize you as a Scout, " .. ply:Nick() ) -- Again, a message in the talk area.
	func()
end -- End this function